package com.itheima.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javassist.expr.NewArray;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.base.Area;
import com.itheima.bos.service.base.IAreaService;
import com.itheima.bos.utils.PinYin4jUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 区域的action
 * @author yanju
 *
 */
public class AreaAction extends BaseAction<Area>{
	@Autowired
	private IAreaService areaServiceImpl;
	//接收参数
	private File file;
	private String fileName;
	private String fileContentType;
	public void setFile(File file) {
		this.file = file;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	@Action(value="area_batchImport")
	public String  area_batchImport() throws FileNotFoundException, IOException{
		//解析文件,得到文件解析对象,将文件数据保存到数据库
		HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
		//得到sheet对象数组
		HSSFSheet sheet = workbook.getSheetAt(0);
		//创建集合存储area对象
		List<Area>areas=new ArrayList<Area>();
		//遍历数组,得到行对象,行对象就是每一隔区域的对象,将行里面的数据放入到area独享中
		for (Row row : sheet) {
			if (row.getRowNum()==0) {//getRowNum得到行号,得到第一行
				//跳过第一行
				continue;
			}else if (row.getCell(0)==null||StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				//空行,就跳过
				continue;
			}
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			//得到省市区的简码
			String province = area.getProvince();
			String city = area.getCity();
			String district = area.getDistrict();
			//去掉后面的一个字
			province = province.substring(0,province.length()-1);	
			city = city.substring(0,city.length()-1);	
			district = district.substring(0,district.length()-1);
			//得到各省市区全名首字母大写数组
			String[] headByString = PinYin4jUtils.getHeadByString(province+city+district);
			//拼接元素
			StringBuffer stringBuffer = new StringBuffer();
			//遍历拼接
			for (String string : headByString) {
				stringBuffer = stringBuffer.append(string); 
			}
			//转成字符串简码存入area对象
			String string = stringBuffer.toString(); 
			area.setShortcode(string);
			
			//存城市简码
			String hanziToPinyin = PinYin4jUtils.hanziToPinyin(city,"");
			area.setCitycode(hanziToPinyin);
			areas.add(area);
		}
		//调用service层存入数据
		areaServiceImpl.save(areas);
		
		return NONE;
		
	}
	
	
	//分页条件查询的方法
	@Action(value="Area_pageQuery",results={@Result(name=SUCCESS,type="json")})
	public String Area_pageQuery(){
		//创建分页对象
		Pageable pageable = new PageRequest(page-1, rows);
		//创建条件对象
		Specification<Area> specification = new Specification<Area>() {

			@Override
			public Predicate toPredicate(Root<Area> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				//创建集合存储查询条件
				List<Predicate>list=new ArrayList<Predicate>();
				//将传入的条件转换成条件对象并存入集合中
				//省对象
				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate p1 = cb.like(root.get("province").as(String.class), "%"+model.getProvince()+"%");
					list.add(p1);
				}
				//市对象
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate p2 = cb.like(root.get("city").as(String.class),"%"+model.getCity()+"%");
					list.add(p2);
				}
				//区对象
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate p3 = cb.like(root.get("district").as(String.class), "%"+model.getDistrict()+"%");
					list.add(p3);
				}
				//将条件集合整合
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		//调用service层
		Page<Area> page=areaServiceImpl.findAll(specification,pageable);
		getPageValueStack(page);
		return SUCCESS;
	}
	
	
	//添加区域的ation
	@Action(value="Area_add",results={@Result(name=SUCCESS,type="json",location="./pages/base/area.html")})
	public String Area_add(){
		//调用service层
		areaServiceImpl.save(model);
		return SUCCESS;
	}
	
	
}
